<template>
  <!-- 人员操作 -->
  <h-modal class="event-modal" v-model="show" :title="type == 'add' ? '添加':'编辑'" @submit="submit">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" size="large">
      <a-row :gutter="100">
        <a-col :span="12">
          <a-form-model-item label="属地" prop="possession">
            <a-select v-model="form.possession" placeholder="请选择">
              <a-select-option :value="item.title" v-for="item in addressArr" :key="item.id">{{ item.title }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="企业名称" prop="companyName">
            <a-input style="width: 100%" :maxLength="50" size="large" v-model="form.companyName" placeholder="请输入企业名称"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="行业代码" prop="industryCode">
            <a-input style="width: 100%" :maxLength="50" size="large" v-model="form.industryCode" placeholder="请输入行业代码"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="统一社会信用代码" prop="unifiedSocialCreditCode">
            <a-input style="width: 100%" size="large" :maxLength="50" v-model="form.unifiedSocialCreditCode" placeholder="请输入统一社会信用代码"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="法定代表人" prop="legalRepresentative">
            <a-input style="width: 100%" size="large" :maxLength="50" v-model="form.legalRepresentative" placeholder="请输入法定代表人"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="注册资金" prop="registeredCapital">
            <a-input-number style="width: 100%" size="large" v-model="form.registeredCapital" placeholder="请输入注册资金"></a-input-number>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="公司业务类型" prop="businessType">
            <a-input style="width: 100%" size="large" :maxLength="50" v-model="form.businessType" placeholder="请输入法定代表人"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="年度营收（万元）" prop="annualRevenue">
            <a-input style="width: 100%" size="large" :maxLength="50" v-model="form.annualRevenue" placeholder="请输入年度营收（万元）"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="员工人数" prop="employeNum">
            <a-input-number style="width: 100%" size="large" v-model="form.employeNum" placeholder="请输入员工人数"></a-input-number>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item label="经营范围" prop="businessScope">
            <a-input style="width: 100%" size="large" :maxLength="50" v-model="form.businessScope" placeholder="请输入经营范围"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item label="经营地址" prop="address">
            <a-input style="width: 100%" size="large" :maxLength="50" v-model="form.address" placeholder="请输入经营地址"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item label="备注">
            <a-textarea style="width: 100%" size="large" :maxLength="300" v-model="form.remark" placeholder="请输入备注"></a-textarea>
          </a-form-model-item>
        </a-col>
      </a-row>

    </a-form-model>
  </h-modal>
</template>

<script>
import moment from 'moment';
// eslint-disable-next-line
import { addInterLv2, updateInterLv2 } from '@/api/db';
import { address } from '@/api/serve';
import cloneDeep from 'lodash/cloneDeep';

export default {
  props: {
    showModal: {
      type: Boolean,
      default: false
    }
  },
  model: {
    prop: 'showModal',
    event: 'update:showModal'
  },
  watch: {
    showModal: {
      handler(val) {
        this.show = val;
        if (val) {
          this.address();
          // detailAccount().then(res => {
          //   this.form = res.data || {};
          //   this.$set(this.form, 'isLayered', !!this.form.isLayered);
          // });
        }
      },
      immediate: true
    },
    show(val) {
      this.$emit('update:showModal', val);
    }
  },
  data() {
    return {
      moment,
      show: false,
      form: {},
      type: 'add',
      rules: {
        possession: [{ required: true, message: '请选择' }],
        companyName: [{ required: true, message: '请输入' }],
        platform: [{ required: true, message: '请输入' }],
        link: [{ required: true, message: '请输入' }]
      },
      addressArr: []
    };
  },
  methods: {
    address() {
      address().then(res => {
        this.addressArr = res.data;
      });
    },
    add() {
      // const that = this;
      this.type = 'add';
      this.$nextTick(() => {
        this.form = {};
        this.$refs.ruleForm && this.$refs.ruleForm.resetFields();
      });
    },
    edit(form) {
      this.type = 'edit';
      this.form = { ...this.form, ...form };
    },
    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (this.type == 'add') {
            addInterLv2(this.form).then(res => {
              this.$message.success('创建成功');
              this.show = false;
              this.$emit('submit', cloneDeep(this.form));
              this.form = cloneDeep({});
            });
          } else if (this.type == 'edit') {
            updateInterLv2(this.form).then(res => {
              this.$message.success('编辑成功');
              this.show = false;
              this.$emit('submit', cloneDeep(this.form));
              this.form = cloneDeep({});
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
};
</script>

<style>

</style>
